local InnerDataManager = require("innerdata.InnerDataManagerFL")
local InnerDefine = require("innerdata.Define")
local CopyDefine = require("copy.Define")
local Copy = require("copy.CopyL")
local ObjMonster = require("core.ObjMonster")
local ObjHuman = require("core.ObjHuman")
local Obj = require("core.Obj")
local ShenbingLogic = require("shenbing.ShenbingLogic")
local OfflinePkExcel = require("excel.OfflinePk")
local BagLogic = require("bag.BagLogic")
local RoleRevive = require("role.RoleRevive")
local ObjPet = require("core.ObjPet")
local PetAi = require("pet.PetAi")
local PetLogic = require("pet.PetLogic")
local Buffer = require("buffer.Buffer")
local TaskCallbacks = require("task.TaskCallbacks")
local TaskDefine = require("task.Define")
local OfficialLogic = require("official.OfficialLogic")

PK_LIST = PK_LIST or {}  -- roomID -> pkuuid

local MONSTER_X = 1587
local MONSTER_Y = 715


function onMonsterDie(killer, monster, monsterID, copyID, roomID)
    if monsterID ~= CopyDefine.OFFLINE_PK_BOSS_ID and monsterID ~= CopyDefine.OFFLINE_PK_BOSS_ID1 then
        return
    end
    Copy.roomFinish(copyID, roomID, CopyDefine.COPY_FINISH_PK_OK, "挑战成功")    
end

-- 挑战者被打死
function onRoleDieCB(atker, human, copyID, roomID)
   Copy.roomFinish(copyID, roomID, CopyDefine.COPY_FINISH_DIE_FAIL, "挑战失败")  
end

local function pkResult(human, uuid, win)
    InnerDataManager.Send2WInnerData(human, InnerDefine.INNERDATA_TYPE_OFFLINE_PK_END, {uuid = uuid, win = win})
    local reward
    if win == 1 then
        reward = OfflinePkExcel.pkReward[1].win
        TaskCallbacks.taskInteractCB(human, TaskDefine.TASK_INTERACT_OFFLINEPK)
    else
        reward = OfflinePkExcel.pkReward[1].fail
    end    
    for i = 1, #reward do  --都是货币类。
        BagLogic.addItem(human, reward[i][1], reward[i][3], reward[i][2],"offlinepk")
    end   
   
end

function onRoomFinish(copyID, roomID, retCode)
    local humanlist = Copy.getCopyHumanList(copyID, roomID)
    if not humanlist or not humanlist[1] then
        return
    end
    pkResult(humanlist[1], PK_LIST[roomID], retCode==CopyDefine.COPY_FINISH_PK_OK and 1 or 0)
end

function onHumanEndCopy(human, copyID, roomID)
    if human.db.hp < 1 then  --死亡选复活
        RoleRevive.reviveNow(human)
    end
    if Copy.roomDatas[copyID][roomID].finishCode then
        return
    end
    --主动退出，算失败
    pkResult(human, PK_LIST[roomID],0)
    OfficialLogic.checkOneCondition(human, "PKRank")
end

function onHumanEnterCopy(human, copyID, roomID, sceneID)
    if ObjHuman.getHp(human) < ObjHuman.getHpMax(human) then
        ObjHuman.setHp(human, 0x7fffffff)
    end
end

function onRoomClear(copyID, roomID)
    PK_LIST[roomID] = nil
end

function onReceivePkData(human, type ,data)
    local roomID = Copy.copyHuman2Room[human._id]
    if not roomID then
        return
    end
    local copyID = Copy.sceneID2CopyID[human.scene_id]
    if not copyID or copyID ~= CopyDefine.COPY_OFFLINE_PK_ID then
        return
    end
    PK_LIST[roomID] = data._id
    if data.account then
      local pkTarget = ObjHuman.create_human(data)
      local monster = ObjMonster.clone(CopyDefine.OFFLINE_PK_BOSS_ID, pkTarget)
      monster.pkTarget = pkTarget
      monster.name = pkTarget.db.name
      monster.lv  = pkTarget.db.lv  

      monster.skills = ShenbingLogic.getSkillList(pkTarget)
      local diffZhanli = data.zhandouli - human.db.zhandouli
      if diffZhanli > 0 then
          local rate = math.floor(diffZhanli*100/human.db.zhandouli)
          local conf
          for i = 2, #OfflinePkExcel.pkBuffer do
              if rate >= OfflinePkExcel.pkBuffer[i-1].rate and rate < OfflinePkExcel.pkBuffer[i].rate then
                 conf = OfflinePkExcel.pkBuffer[i-1]
                 break
              end
          end
          if rate > OfflinePkExcel.pkBuffer[1].rate then
            if not conf then
              conf = OfflinePkExcel.pkBuffer[#OfflinePkExcel.pkBuffer]
            end
            for v in ipairs(conf.buffers) do
              Buffer.add(conf.buffers[v], monster, monster)
            end
          end
      end
      ObjMonster.doCalc(monster)
      ObjMonster.setHp(monster, ObjMonster.getHpMax(monster))
      ObjMonster.enterScene(monster,human.scene_id, MONSTER_X,  MONSTER_Y )
      --神将出
      if data.petCombatId ~= 0 then
        monster.db = data
        local pet = ObjPet.create(monster, data.petCombatId)
        Obj.enterScene(pet, human.scene_id, MONSTER_X-50, MONSTER_Y)          
        if pet.db.hp == 0 then
            pet.is_hide = true
            PetAi.send_hide(pet)
        end
        pet.skill = {}
        PetLogic.getCanUseSkillHuman(monster, data.petCombatId, pet.skill)
        monster.db = nil
      end
    else
      local monster = ObjMonster.create(CopyDefine.OFFLINE_PK_BOSS_ID1)
      monster.name = data.name
      monster.offlinepkRank = data.rank
      ObjMonster.enterScene(monster,human.scene_id, MONSTER_X,  MONSTER_Y  )
    end
end
InnerDataManager.Register(InnerDefine.INNERDATA_TYPE_OFFLINE_PK_GET_DATA,onReceivePkData)

